from flask import Flask, request, render_template

from KWMusic import KWMusic
from SeachKWMusic import SearchKWMusic
from Fpw import Fpw
from Mysql import Mysql

app = Flask(__name__)

'''
参考网站：
https://www.fangpi.net/

'''
# 初始化数据库
m = Mysql()

@app.route('/select_playlist_by_id', methods=["GET"])
def select_playlist_by_id():
    id = request.form.get("id")
    result = m.select_playlist_by_id(id)
    return result

@app.route('/select_playlist', methods=["GET"])
def select_playlist():
    result = m.select_playlist()
    return result

@app.route('/del_playlist', methods=["POST"])
def del_playlist():
    id = request.form.get("id")
    result = m.del_playlist(id)
    return result


@app.route('/add_playlist', methods=["POST"])
def add_playlist():
    song_name = request.form.get("song_name")
    href = request.form.get("href")
    result = m.add_playlist(song_name, href)
    return result
@app.route('/findmusic_kg', methods=["POST"])
def findmusic_kg():
    keyword = request.form.get("keyword")
    page = request.form.get("page")
    pagesize = request.form.get("pagesize")
    if keyword != None:
        music = SearchKWMusic()
        search = music.search(keyword, page, pagesize, "kugou")
        return search

    return {"msg": "缺少关键字"}

@app.route('/findmusic_kw', methods=["POST"])
def findmusic_kw():
    keyword = request.form.get("keyword")
    page = request.form.get("page")
    page = str(int(page) - 1)
    pagesize = request.form.get("pagesize")
    if keyword != None:
        music = SearchKWMusic()
        search = music.search(keyword, page, pagesize, "kuwo")
        return search

    return {"msg": "缺少关键字"}

@app.route('/findmusic_fpw', methods=["POST"])
def findmusic_fpw():
    keyword = request.form.get("keyword")
    if keyword != None:
        music = Fpw()
        search = music.search(keyword)
        return search

    return {"msg": "缺少关键字"}


@app.route('/music_info', methods=["POST"])
def music_info():
    id = request.form.get("id")
    hash = request.form.get("hash")
    path = KWMusic().getPath(id)
    return path

@app.route('/music_info_fpw', methods=["POST"])
def music_info_fpw():
    href = request.form.get("href")
    name = request.form.get("name")
    id = request.form.get("id")
    path = ""
    if id != 'undefined':
        path = m.select_playlist_by_id(id)
    else:
        path = Fpw().getPath(href, name)

    return path



@app.route('/', methods=["GET"])
def index():
    '''
    主页接口
    :return:
    '''
    return app.send_static_file('index.html')


@app.route('/play', methods=["GET"])
def play():
    '''
    播放接口
    :return:
    '''
    src = request.args.get("src")
    name = request.args.get("name")
    word = request.args.get("word")
    return render_template("index.html", src=src, name=name, word=word)


@app.route('/p', methods=["GET"])
def play_qq():
    id = request.args.get("id")
    data = KWMusic().getPath(id)
    return render_template("play.html", data=data)


if __name__ == '__main__':
    app.run(host="0.0.0.0", port=6688)
